﻿#region

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using Imdb;
using MediaSage.Lib.Utilities;
using ICSharpCode.SharpZipLib;

#endregion

namespace GetAllMovieTitles
{
    public class Program
    {
        #region Methods

        public static void Main(string[] args)
        {
            IList<string> Titles;
            
            // Get Title List from IMDB
            /*
            WebClient request=new WebClient();
            byte[] fileData = request.DownloadData("ftp://ftp.sunet.se/pub/tv+movies/imdb/movies.list.gz");

            FileStream file = File.Create("movies.list.gz");
            file.Write(fileData, 0, fileData.Length);
            file.Close();

            FileStream Input = File.OpenRead("movies.list.gz");
            FileStream Output = File.Create("movies.list");

            
            GZipInputStream gzInputStream=new GZipInputStream(Input);
            byte[] buffer=new byte[100000];
            int currentIndex = 0;
            int count = buffer.Length;

            while (true)
            {
                int numRead = gzInputStream.Read(buffer, currentIndex, count);
                if (numRead <= 0)
                {
                    break;
                }
                Output.Write(buffer,0,buffer.Length);                
            }
            Input.Close();
            Output.Flush();
            Output.Close();
            gzInputStream.Close();*/

            Regex lineRegex=new Regex("\"(?<TITLE>.+)\".*{(?<EPISODE>.+)}",RegexOptions.Compiled);
            Regex lineRegex2=new Regex(@"(?<TITLE>.+) \(.{4}\)");
            Regex removeRegex=new Regex("(.+)",RegexOptions.Compiled);
            var tt = new TernaryTree();
            TextReader tr=new StreamReader("movies.list");
            string line;
            int lineNo = 0;
            while ((line = tr.ReadLine()) != null)
            {
                MatchCollection mc = lineRegex.Matches(line);
                foreach (Match match in mc)
                {
                    string Title = match.Groups["TITLE"].Captures[0].Value;
                    string Episode = Regex.Replace(match.Groups["EPISODE"].Captures[0].Value, "\\(.+\\)", "");
                    if (!String.IsNullOrWhiteSpace(Title))
                    {
                        tt.Add(Title);
                        if(!String.IsNullOrWhiteSpace(Episode))
                        {
                            tt.Add(Episode);
                        }
                    }                        
                    else
                    {
                        MatchCollection mc2 = lineRegex2.Matches(line);
                        {
                            foreach(Match m2 in mc2)
                            {
                                string Title2 = match.Groups["TITLE"].Captures[0].Value;
                                if(!String.IsNullOrWhiteSpace(Title2))
                                {
                                    tt.Add(Title2);
                                }
                            }
                        }
                    }
                }
                lineNo++;
                if (lineNo % 1000 == 0)
                {
                    Console.WriteLine(lineNo);
                }
            }
            
            tt.Save(Path.Combine(Directory.GetCurrentDirectory(),
                                 @"..\..\..\MediaSageLib\FileNameProvider\IMDBTitles.dat"));

            // Save a unigram model of the titles

        }

        private void WriteTitles(IList<string> titles)
        {
            var file = new StreamWriter("VideoTitles.txt", false);
            foreach (string title in titles)
            {
                file.WriteLine(title);
            }
            file.Close();                
        }

        #endregion Methods
    }
}